import api from '@/api/good'
import { Message } from 'element-ui'
const good = {
    state: {
        goodDetail: {
            sellDate: '2019/01', //没有
            sellerId: 2,
            isCollected: 0,
            goods_name: '',
            type_name: '',
            goods2content_content: '',
            goods_price: '',
            shop_name: '',
            detail: {},
            goods_status: null,
            des: {
              desTitle: '',
              desContent: ''
            },
            log: {
              logTitle: '',
              logContent: ''
            }
        },
    },

    mutations: {
        saveDetail: (state, payload) => {
            const newData = {
                detail: {},
                des: {
                    desTitle: '',
                    desContent: ''
                },
                log: {
                    logTitle: '',
                    logContent: ''
                }
            };
            payload.propertyList.forEach(item => {
                newData.detail[`${item.property_name}`] = item.propertyvalue_name;
            });
            newData.des.desTitle = `${payload.goodsInfo[0].goods_name}描述`;
            newData.des.desContent = payload.goodsInfo[0].goods_description;
            newData.log.logTitle = `${payload.goodsInfo[0].goods_name}更新日志`;
            newData.log.logContent = payload.goodsInfo[0].goods_updateLog;
            state.goodDetail = Object.assign(state.goodDetail,payload.goodsInfo[0],newData);
            state.goodDetail.isCollected = payload.isCollect[0];
            state.goodDetail.sellerId = payload.goodsInfo[0].shop_id
            
        },
        changeStatu: (state,payload) => {
            state.goodDetail.goods_status = payload;
        },
        changeIsCollected: (state, isCollected) => {
            state.goodDetail.isCollected = isCollected;
        },
    },

    actions: {
        //宝贝详情接口
        async fetchGoodsDetail({ commit },payload) {
            const { code, extend:{ result:{ propertyList, goodsInfo, isCollect } } } = await api.getGoodsDetail(payload);
            if(code === 200){
                commit('saveDetail',{ propertyList, goodsInfo, isCollect });
            }
        },
        //收藏商品
        async fetchCollect({ commit },payload) {
            const { code, extend: { message } } = await api.collectGoods(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '已添加至收藏。',
                    type: 'success',
                });
                commit('changeIsCollected',1);
            }else{
                Message({
                    showClose: true,
                    message: message || '收藏宝贝失败, 请重试。',
                    type: 'error',
                });
            }
        },
    }
}

export default good